package com.ijava.base.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ijava.base.dao.DictionaryDefineDAO;
import com.ijava.base.domain.DictionaryDefine;
import com.ijava.base.service.DictionaryDefineService;

public class DictionaryDefineServiceImpl implements DictionaryDefineService {

	private static final Logger logger = LoggerFactory.getLogger(DictionaryDefineServiceImpl.class);
	private DictionaryDefineDAO dao;

	// private DataSource dataSource;

	// public DataSource getDataSource() {
	// return dataSource;
	// }
	//
	// public void setDataSource(DataSource dataSource) {
	// this.dataSource = dataSource;
	// }

	public DictionaryDefineDAO getDao() {
		return dao;
	}

	public void setDao(DictionaryDefineDAO dao) {
		this.dao = dao;
	}

	public void remove(String id) throws Exception {
		dao.remove(id);
	}

	public void mutilRemove(DictionaryDefine dictionaryDefine) throws Exception {
		try {
			String[] keys = dictionaryDefine.getKeys();
			if(null == keys)
				return;
			for (int i = 0; i < keys.length; i++) {
				remove(keys[i]);
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

	public void save(DictionaryDefine dictionaryDefine)
			throws Exception {
		dao.save(dictionaryDefine);
	}

	public void update(DictionaryDefine dictionaryDefine) throws Exception {
		dao.update(dictionaryDefine);
	}

	public void saveOrUpdate(DictionaryDefine dictionaryDefine)
			throws Exception {
		dao.saveOrUpdate(dictionaryDefine);
	}

	public DictionaryDefine get(String id) throws Exception {
		return dao.get(id);
	}

	public List find(DictionaryDefine dictionaryDefine) throws Exception {
		try {
			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer(" where 1 = 1");
			if (StringUtils.hasLength(dictionaryDefine.getDefineType())) {
				condition.append(" and defineType = ?");
				paramList.add(dictionaryDefine.getDefineType());
			}
			String queryString = "from " + DictionaryDefine.class.getName()
					+ condition.toString();
			return dao.find(queryString, paramList);
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

	public List findWithPaging(DictionaryDefine dictionaryDefine)
			throws Exception {
		try {
			int firstRow = (dictionaryDefine.getPageNo() - 1)
					* dictionaryDefine.getPageSize();
			int maxRow = dictionaryDefine.getPageSize();

			// JdbcTemplate template = new JdbcTemplate(dataSource);
			// int total =
			// template.queryForInt("SELECT COUNT(1) FROM TABLE_NAME");
			// dictionaryDefine.setTotal(total);

			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer(" where 1 = 1");
			if (StringUtils.hasLength(dictionaryDefine.getDefineCode())) {
				condition.append(" and defineCode like ?");
				paramList.add("%" + dictionaryDefine.getDefineCode() + "%");
			}
			if (StringUtils.hasLength(dictionaryDefine.getDefineName())) {
				condition.append(" and defineName like ?");
				paramList.add("%" + dictionaryDefine.getDefineName() + "%");
			}
			if (StringUtils.hasLength(dictionaryDefine.getDefineType())) {
				condition.append(" and defineType = ?");
				paramList.add(dictionaryDefine.getDefineType());
			}

			List list = dao.find("select count(*) from "
					+ DictionaryDefine.class.getName() + condition.toString(),
					paramList);
			dictionaryDefine.setTotal(list.size());

			String queryString = "from " + DictionaryDefine.class.getName()
					+ condition.toString();
			return dao.findWithPaging(queryString, paramList, firstRow, maxRow);
		} catch (Exception e) {
			logger.error("", e);

			throw e;
		}
	}

}
